import java.util.Arrays;

public class MyArrays {

    public static int index;
    public static int[] data;


    public static void main(String[] args) {
        data = new int[5];
        index = 0;
        double radio = 0.5;

        System.out.println("刚初始化数组时，数组长度为:" + data.length);
        System.out.println("刚初始化数组时，数组内容为:" + Arrays.toString(data));


        for (int i = 0; i < 10 ; i++) {
            extendArray(radio);
            data[index] = i;
            index += 1;
        }

        System.out.println("添加完10个元素后，数组长度为:" + data.length);
        System.out.println("加完10个元素后，数组内容为:" + Arrays.toString(data));
    }


    /**
     * 给数组扩容,当数组比例达到radio时，给数组扩容
     */
    public static void extendArray(double radio) {
        int length = data.length;
        int threshold = ((int) Math.nextUp(length * radio));
        if (threshold == (index + 1)) {
            data = Arrays.copyOf(data, ((int) ((1+radio) * length)));
        }
    }
}
